package com.yc.demo.servlet;

import java.awt.Window;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 
 * http://localhost/servlet/test.s
 *
 */
@WebServlet("/test.s")
public class TestServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest requset, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		String year = requset.getParameter("year");
		String month = requset.getParameter("month");
		String day = requset.getParameter("day");
		int y = Integer.parseInt(year);
		int m = Integer.parseInt(month);
		int d = Integer.parseInt(day);
		if(y<=0||y>2021) {
			response.getWriter().append("<script>alert('年份输入格式错误')</script>");
			return;
		}
		if(m<=0||m>12) {
			response.getWriter().append("<script>alert('月份输入格式错误')</script>");
			return;
		}
		if(m==2&&d>28) {
			response.getWriter().append("<script>alert('日期输入格式错误')</script>");
			return;
		}
		if(m%2==0&&d>30) {
			response.getWriter().append("<script>alert('日期输入格式错误')</script>");
			return;
		}
		if(m%2==1&&d>31) {
			response.getWriter().append("<script>alert('日期输入格式错误')</script>");
			return;
		}
		try {
			// 1.日期格式
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			// 2.某天的日期
			Date da1 = sdf.parse(year+"-"+month+"-"+day);
			// 3.输出间隔天数 getTime获取的是毫秒
			long t = (new Date().getTime()-da1.getTime())/24/60/60/1000;
			response.getWriter().append(year+"年"+month+"月"+day+"天"
			+"距今天已过去"+t+"天");
			String[] arr = {"星期天","星期一","星期二","星期三","星期四","星期五","星期六"};
			PrintWriter out = response.getWriter();
			int i = 0;
			Calendar c=Calendar.getInstance();
			c.setTime(da1);
			int weekday=c.get(Calendar.DAY_OF_WEEK);
			while(arr[i]!=null) {
				if(i+1==weekday) {
					out.append("<br>"+arr[i]+"<input type='radio' name = 'group'"
							+ " values = "+arr[i]+" checked = 'checked'>");
				}else {
					out.append("<br>"+arr[i]+"<input type='radio' name = 'group'"
							+ " values = "+arr[i]+">");
				}
				i++;
			}
			
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void doPost(HttpServletRequest requset, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(requset, response);
	}
}
